package com.bjb.filter;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 处理跨域过滤器
 *
 */
@Component
public class AcrossFilter extends AbstractFilter {

	/**
	 * Log4j控制台输出
	 * 请查看Log4j配置文件，详细Log4j appender请查看log4j官方文档
	 */
	public static final Logger logger = Logger.getRootLogger();

	@Override
	protected void doFilter(HttpServletRequest request,
			HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		logger.info("across跨域过滤器执行过滤业务2...");
	    response.setHeader("Access-Control-Allow-Origin", "*");
	    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
	    response.setHeader("Access-Control-Allow-Credentials", "true");
	    response.setHeader("Access-Control-Max-Age", "3600");
	    response.setHeader("Access-Control-Allow-Headers","withCredentials,SESSIONID,Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With");
	    chain.doFilter(request, response);
	}

	@Override
	protected void doInit(FilterConfig config) {
		logger.info("across跨域过滤器被创建！");
	}
}
